import { Service, Context } from 'egg';

export default class SafeCode extends Service {
  private mode: string;
  constructor(ctx: Context) {
    super(ctx);
    this.mode = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  }

  public async generate() {
    const length = this.mode.length;
    let res = '';
    for (let i = 0; i < 4; i++) {
      const randomIndex = Math.floor(Math.random() * length);
      res += this.mode[randomIndex];
    }
    return res;
  }
}
